package com.jww.controller;

import com.github.pagehelper.PageHelper;
import com.jww.domain.Student;
import com.jww.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {
    private SqlSession session;
    private InputStream is;
    private StudentMapper mapper;

    @Before
    public void before() throws IOException {
        //1
        is = Resources.getResourceAsStream("mybatisConfig.xml");

        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(is);

        session = fac.openSession();

        //4
        mapper = session.getMapper(StudentMapper.class);
    }

    @After
    public void after() {
        //6事务提交
        session.commit();
        //7关
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        session.close();
    }

    @Test
    public void testFindAll() throws IOException {
        //1
        InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");

        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(is);

        SqlSession session = fac.openSession();

        //4
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        //5
        List<Student> all = mapper.findAll();
        //6事务提交
        session.commit();
        //7关
        is.close();
        session.close();

    }

    @Test
    public void testFind() {
        List<Student> list = mapper.findAll();
        PageHelper.startPage(1, 2);

    }

    //动态sql语句 selectByName
    @Test
    public void testFindByName() {

        List<Student> list = mapper.findByName(" ");
        System.out.println(list);

    }

    //selectByNameList
    @Test
    public void testFindByNameList() {
        ArrayList list = new ArrayList();
        list.add(2);
        List<Student> byNameList = mapper.findByNameList(list);
        System.out.println(byNameList);

    }

    //selectByNameArray
    @Test
    public void testFindByNameArray() {
        String[] ss = {"sun"};
        List<Student> byNameArray = mapper.findByNameArray(ss);
        System.out.println(byNameArray);

    }

    //selectByIds
    @Test
    public void testFindByIds() {
        Integer[] ids = {1, 2};
        mapper.deleteByIds(null);

    }

}
